@extends('layouts.back_stage')
@section('content')

    <link rel="stylesheet" href="{{env('CDN_DOMAIN_NAME')}}/css/createPro.css"/>
    <link rel="stylesheet" href="{{env('CDN_DOMAIN_NAME')}}/css/clothInfo.css"/>
    <link href="{{env('CDN_DOMAIN_NAME')}}/css/jcDate.css" rel="stylesheet">
    <style>
        .form_li_surface select{
            float:none;
            margin-right:10px;
        }
        .form_ul_surface table input{
            border:none;
            text-align: center;
        }
        .form_ul_surface table select{
            border:none;
            text-align: center;
        }
        .save_button{
            margin-right:10px;
        }
        .address {
            border-color: #cfdadd;
            border-radius: 2px;
            width: 25%;
            height: 30px;
            padding: 6px 10px;
            font-size: 12px;
            line-height: 1.42857143;
            color: #555;
            background-color: #fff;
            background-image: none;
            border: 1px solid #cfdadd;
            box-shadow: none;
            -webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s;
            -o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
        }
        .login_a img{width:15px;height:15px;display:block;}
        .th_unit{
            padding-right:15px;
            padding-left:15px;
        }
        .check_submit{
            margin-top:50px;
        }
        .check_submit li{
            width:100%;
        }
        .check_submit li button{
            float: right;
        }
        .check_submit li h3{
            width:100%;
            text-align: right;
            display: block;
        }
        .form_li span{
            color:red;
        }
        .model-container{
            width:100%;
            height:100%;
            position:absolute;
            top:0;
            left:0;
            background: rgba(0,0,0,0.5);
            z-index: 999;
            display: none;
        }
        .model{
            width:50%;
            position:absolute;
            top:20%;
            left:25%;
            z-index: 1000;
            background-color:white;
            border-radius: 10px;
            padding:20px;
        }
        .model .fab{
            width:100%;
        }
        .model button{
            float:right;
            margin-top:20px;
            margin-left:10px;
        }
        .check_surface_hand input{
            width:50px;
            border:none;
            outline:none;
            text-align: center;
        }
    </style>

    <div class="container">
        <div class="row">
            <div style="margin:30px;"><h3>添加采购信息</h3></div>
            <div style="width:90%;margin-left:60px;">
                采购单号:<span class="pur_list_id">MLCG-@if(strlen($purchase->id) == 1)00000
                    @elseif(strlen($purchase->id) == 2)0000
                    @elseif(strlen($purchase->id) == 3)000
                    @elseif(strlen($purchase->id) == 4)00
                    @elseif(strlen($purchase->id) == 5)0
                    @endif</span>{{ $purchase->id }}
                <hr>
                    <div class="form-group">
                        <ul class="form_ul">
                            <li class="form_li" style="width:220px;"><span>*</span>供应商：
                                <select class="sel_inp"  id="supplier" autocomplete="on">
                                    @if($purchase->supplier_name)
                                        <option value="{{ $purchase->supplier }}">{{ $purchase->supplier_name }}</option>
                                        @else
                                        <option value="">请选择</option>
                                        @endif
                                    @if(count($fabric) > 0)
                                        @foreach($fabric as $v)
                                            <option value="{{ $v->id }}">{{ $v->name }}</option>
                                            @endforeach
                                        @endif
                                </select>
                            </li>
                            <li class="form_li" style="width:200px;"><span>*</span>销售季节：
                                <select class="sel_inp" style="width:110px;" id="season">
                                    @if($purchase->season)
                                        <option value="{{ $purchase->season }}">{{ $purchase->season }}</option>
                                        @else
                                        <option value="">请选择</option>
                                        @endif

                                    <option value="春">春</option>
                                    <option value="夏">夏</option>
                                    <option value="秋">秋</option>
                                    <option value="冬">冬</option>
                                </select>
                            </li>
                            <li class="form_li" style="width:220px;" ><span>*</span>采购类型：
                                <select class="sel_inp" id="purchase_status">

                                    @if($purchase->purchase_status == 1)
                                        <option value="1">计划采购</option>
                                        <option value="2">提前采购</option>
                                        @elseif($purchase->purchase_status == 2)
                                        <option value="2">提前采购</option>
                                        <option value="1">计划采购</option>
                                        @else
                                        <option value="">请选择</option>
                                        <option value="1">计划采购</option>
                                        <option value="2">提前采购</option>
                                     @endif

                                </select>
                            </li>
                        </ul>
                        <ul class="form_ul">
                            <li class="form_li" style="width:220px;">
                                <span>*</span>采购日期:
                                <input class="jcDate form-control mark_date" id="purchase_date"  style="width: 130px;float:right;" placeholder="" autocomplete="off" value="{{ $purchase->purchase_date }}"  />
                            </li>
                        </ul>
                        <ul class="form_ul">
                            <li class="form_li" style="width:220px;">合同号：
                                <input class="form_inp" type="text" placeholder="合同号" value="{{ $purchase->contract_code }}" id="contract_code" /></li>
                            </li>
                            <li class="form_li" style="width:200px;">合同签订人：
                                <input class="form_inp" type="text" style="width:110px;" placeholder="合同签订人" value="{{ $purchase->contract_name }}" id="contract_name" /></li>
                            </li>
                            <li class="form_li" style="width:220px;">备注：<input class="form_inp" type="text" value="{{ $purchase->remarks }}" id="remarks" /></li>
                        </ul>
                    </div>
                <div style="width:100%;border-bottom: 1px solid #cccccc;height:20px;margin-bottom: 20px;"></div>
                <ul class="form_ul">
                    <li class="form_li" style="clear: both;">
                        <h4 style="float:left;">包含面辅料</h4>
                        <button type="button" class="plus surface_plus">+</button>
                        <button type="button" class="minus surface_minus">-</button>
                    </li>
                </ul>
                <ul class="form_ul_surface">
                    <table class="pantone" border="1" cellspacing="0" cellpadding="0">
                        <thead>
                        <tr>
                            <td class="col-xs-1">面辅料号</td><td class="col-sm-1">颜色</td><td class="col-md-1">投产需求量</td><td class="col-md-1">物控量</td>
                            <td class="col-md-1">采购量</td><td class="col-md-1">剩余库存</td><td class="th_unit">单位</td><td class="col-md-1">采购价</td>
                            <td class="col-md-1">总金额</td><td class="col-md-1">服务款号</td><td class="col-md-1">备注</td><td class="col-md-2">操作</td>
                        </tr>
                        </thead>
                        <tbody>
                        @if(count($pur_details) > 0)
                            @foreach($pur_details as $v)
                            <tr id="{{ $v->id }}">
                                <td class='cloth_code'><select class='form-control surface_id'><option value="{{ $v->cloth_code }}">{{ $v->cloth_code }}</option><option value=''></option><option value=''></option><option value=''></option></select></td>
                                <td class='surface_color'><select class='form-control'><option value=''>{{ $v->color }}</option></select></td>
                                <td class='surface_deman'><input class='form-control' type='text' value='{{ $v->demand }}%'/></td>
                                <td class='surface_mc'><input class='form-control' type='text'  value="{{ $v->control }}"/></td>
                                <td class='surface_purchase'><input class='form-control' type='text'  value="{{ $v->purchase }}" /></td>
                                <td class="surplus_stock"><input class='form-control' type='text' value="0" /></td>
                                <td class='surface_unit'><input class='form-control' type='text' value="{{ $v->unit }}" /></td>
                                <td class='surface_pur_price'><input class='form-control' type='text' value="{{ $v->purchase_price }}" /></td>
                                <td class='surface_money'><input class='form-control' type='text' value="{{ $v->total_price }}"/></td>
                                <td class="surface_to_girard"><input class="surface_to_girard_child" abc="{{ $v->cloth_ids }}" type="text" class="form-control" value="{{ $v->girard }}"></td>
                                <td class='surface_remarks'><input class='form-control' type='text' value="{{ $v->remarks }}"/></td>
                                <td class="button_box"><button class='btn btn-info save_button'>修改</button><button class='btn btn-info remove_button'>删除</button></td>
                            </tr>
                            @endforeach
                            @endif
                        </tbody>
                    </table>
                </ul>
                <ul class="form_ul check_submit">
                    <li class="form_li">
                        <h3 class="sum_of_num">总计数量：<span>0</span></h3>
                        <h3 class="sum_of_money">合计金额：<span>0</span></h3>
                    </li>
                    <li class="form_li">
                        <button class="btn btn-info save_all">保存</button>
                    </li>
                    <li class="form_li">
                        <a class="btn btn-success" style="margin-top:15px;float:right;" href="/purchase/purchaseList">返回上一页</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
    </div>
    <div class="model-container">
        <div class="model">
            <span class="remove-model"></span>
            <table class="fab model_data" border="1" cellspacing="0" cellpadding="0">
                <thead>
                    <tr>
                        <td><input type="checkbox" class="goAllChoose" disabled="disabled"></td><td>投产款号</td><td>单件用量</td><td>件数</td>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
            <button class="btn btn-info" onclick="cencelModel()">取消</button><button class="btn btn-success save_girard">确认</button>
        </div>
    </div>
    <script type="text/javascript">
        //初始化计算
        $(document).ready(function () {
            if($('.pantone tbody tr').length > 0){
                var sumNum = 0;
                for(var m = 0;m<$('.surface_purchase input').length;m++){
                    sumNum += parseInt($('.surface_purchase input').eq(m).val());
                }
                $('.sum_of_num span').text(sumNum);
                sumMoney();
            }
        });
        //金额求和
        function sumMoney(){
            var sumMoney = 0;
            for(var i = 0;i<$('.surface_money input').length;i++){
                sumMoney += parseInt($('.surface_money input').eq(i).val());
            }
            $('.sum_of_money span').text(sumMoney);
        };
        //日期选择器引导
        $(function (){
            $(".jcDate").jcDate({
                IcoClass : "jcDateIco",
                Event : "click",
                Speed : 300,
                Left : 0,
                Top : 28,
                format : "-",
                Timeout : 100000
            });
        });
        //获取URL参数
        (function ($) {
            $.getUrlParam = function (name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return null;
            }
        })(jQuery);
        //点击面辅料号 获取面辅料号列表
        $(document).on('click','.surface_id',function () {
            var that0 = $(this);
            var op;
            var supplier = $('#supplier').val();
            var that_choose_text = $(this).find("option:selected").text();
            var that_choose_value = $(this).find("option:selected").val();
            if($('#supplier').val() == ''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请选择供应商");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                    },
                    url:'/cloth/getFabricCloth',
                    type:'post',
                    dataType:'json',
                    data:{
                        pid:supplier
                    },
                    success:function (event) {
                        if(event.info.length > 0)
                        {
                            $(event.info).map(function () {
                                op += "<option value="+this.id+">"+this.fabric_num+"</option>";
                            })
                        }
                        that0.html("<option value="+that_choose_value+">"+that_choose_text+"</option>"+op);
                    }
            });
        });
        //选择完面辅料号获取颜色
        $(document).on('change','.surface_id',function () {
            var that_ = $(this);
            var surface_id = that_.val();
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                url:'/cloth/getClothColor',
                type:'post',
                dataType:'json',
                data:{
                    pid:surface_id
                },
                success:function (event) {
                    if(event.success = 'true'){
                        that_.parent().siblings('.surface_color').children('select').children().remove();
                        that_.parent().siblings('.surface_color').children('select').append("<option value=''>请选择</option>");
                        for(var i =0;i<event.info.length;i++){
                            that_.parent().siblings('.surface_color').children('select').append("<option value=''>"+event.info[i].color_name+"</option>")
                        }
                    }
                }
            })
        });
        //点击包含面辅料+
        $('.surface_plus').click(function () {
            var newOptions;
            var newTrBox = "<tr><td class='cloth_code'><select class='form-control surface_id'><option value=''>请选择</option><option value=''></option><option value=''></option></select></td><td class='surface_color'><select class='form-control'><option value=''>请选择</option></select></td><td class='surface_deman'><input class='form-control' type='text' value='0%'/></td><td class='surface_mc'><input class='form-control' type='text'/></td><td class='surface_purchase'><input class='form-control' type='text'/></td><td class='surplus_stock'><input class='form-control' type='text' value='' /></td><td class='surface_unit'><input class='form-control' type='text' value='米' /></td><td class='surface_pur_price'><input class='form-control' type='text'/></td><td class='surface_money'><input class='form-control' type='text'/></td><td class='surface_to_girard'><input type='text' class='surface_to_girard_child form-control' value='' /></td><td class='surface_remarks'><input class='form-control' type='text'/></td><td><button class='btn btn-info save_button'>保存</button><button class='btn btn-info remove_button'>删除</button></td></tr>";
            if($('#supplier').val()==''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请先把上方必填项填完");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            $('.pantone').children('tbody').prepend(newTrBox);
            var supplier = $('#supplier').val();
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                url:'/cloth/getFabricCloth',
                type:'post',
                dataType:'json',
                data:{
                    pid:supplier
                },
                success:function (event) {
                    if(event.success = 'true'){
                        $('.surface_id:first').children().remove();
                        $('.surface_id:first').append("<option value=''>请选择</option>");
                        for(var i = 0;i<event.info.length;i++) {
                            $('.surface_id:first').append("<option value=" + event.info[i].id + ">" + event.info[i].fabric_num + "</option>");
                            newOptions += "<option value=" + event.info[i].id + ">" + event.info[i].fabric_num + "</option>"
                        }
                        newTrBox = "<tr><td class='cloth_code'><select class='form-control surface_id'><option value=''>请选择</option>"+newOptions+"<option value=''></option></select></td><td class='surface_color'><select class='form-control'><option value=''>请选择</option></select></td><td class='surface_deman'><input class='form-control' type='text' value='0%'/></td><td class='surface_mc'><input class='form-control' type='text'/></td><td class='surface_purchase'><input class='form-control' type='text'/></td><td class='surplus_stock'><input class='form-control' type='text' value='' /></td><td class='surface_unit'><input class='form-control' type='text' value='米' /></td><td class='surface_pur_price'><input class='form-control' type='text'/></td><td class='surface_money'><input class='form-control' type='text'/></td><td class='surface_to_girard'><input type='text' class='surface_to_girard_child form-control' value='' /></td><td class='surface_remarks'><input class='form-control' type='text'/></td><td><button class='btn btn-info save_button'>保存</button><button class='btn btn-info remove_button'>删除</button></td></tr>";
                    }
                }
            });
        });
        //点击包含面辅料-
        $('.surface_minus').click(function () {
            var getNow_money=$('.sum_of_money span').text();
            var getNow_num=$('.sum_of_num span').text();
            var getSelf_money=$('.pantone tbody tr:first .surface_money input').val();
            var getSelf_num=$('.pantone tbody tr:first .surface_purchase input').val();
            if($('.pantone tbody tr').length > 0){
                $(".oText").text("确认要删除吗？");
                $(".oMask").show();
                $(".oAnim").show();
                $(".oBtn0").attr('id','');
                $(".oBtn0").attr('id','oRefund');
                /*点确定*/
                var id = $('.pantone tbody tr').eq(0).attr('id');
                var getNow_money=$('.sum_of_money span').text();
                var getNow_num=$('.sum_of_num span').text();
                var that_ = $('.pantone tbody tr').eq(0);
                var getSelf_money=that_.children('.surface_money').children().val();
                var getSelf_num=that_.children('.surface_purchase').children().val();
                $(document).on('click','#oRefund',function(){
                    $(".oMask").hide();
                    $(".oAnim").hide();
                    if(id == undefined){
                        $('.sum_of_money span').text(getNow_money-getSelf_money);
                        $('.sum_of_num span').text(getNow_num-getSelf_num);
                        that_.remove()
                    }else{
                        $.ajax({
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                            },
                            url:'/purchase/deletePurInfo',
                            type:'post',
                            dataType:'json',
                            data:{
                                id:id
                            },
                            success:function(res){
                                if(res.success = "true"){
                                    that_.remove();
                                    $(".faceImg").attr("src","/img/laugh.png");
                                    $(".oHandle").text("删除成功");
                                    $(".oSuccess").show(0).delay(1000).hide(0);
                                }
                            }
                        })
                    }
                });
            }
        });
        //选择完颜色获取剩余库存
        $(document).on('change','.surface_color select',function () {
            var that_ = $(this);
            var getClothCode = that_.parent().siblings('.cloth_code').children().val();
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                url:'/purchase/getOthercloth',
                type:'post',
                dataType:'json',
                data:{
                    cloth_code:getClothCode,
                    color:that_.find("option:selected").text()
                },
                success:function (res) {
                    if(res.success){
                        that_.parent().siblings('.surplus_stock').children().val(res.info);
                    }
                }
            })
        });
        //保存单行
        $(document).on('click','.save_button',function () {
            var that_= $(this);
            var cloth_code = that_.parent().siblings('.cloth_code').find('option:selected').text();
            var color = that_.parent().siblings('.surface_color').find('option:selected').text();
            var demand = that_.parent().siblings('.surface_deman').children().val();
            var control = that_.parent().siblings('.surface_mc').children().val();
            var purchase = that_.parent().siblings('.surface_purchase').children().val();
            var unit = that_.parent().siblings('.surface_unit').children().val();
            var purchase_id = $.getUrlParam('id');
            var purchase_price = that_.parent().siblings('.surface_pur_price').children().val();
            var total_price = that_.parent().siblings('.surface_money').children().val();
            var surface_girard = that_.parent().siblings('.surface_to_girard').children().val();
            var remarks = that_.parent().siblings('.surface_remarks').children().val();
            var id = that_.parent().parent().attr('id');
            var cloth_ids = that_.parent().siblings('.surface_to_girard').children().attr('abc');
            if(cloth_code == '' || cloth_code == '请选择' || color == '' || color == '请选择' || demand == '' || control == ''  || purchase == ''  || unit == '' || purchase_id == ''|| purchase_price == '' || total_price == ''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请填写完整");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            };
            if(that_.text() == '保存'){
                $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                    },
                    url:'/purchase/createPurInfo',
                    type:'post',
                    dataType:'json',
                    data:{
                        cloth_code:cloth_code,
                        color:color,
                        demand:demand,
                        control:control,
                        purchase:purchase,
                        unit:unit,
                        purchase_id:purchase_id,
                        purchase_price:purchase_price,
                        total_price:total_price,
                        girard:surface_girard,
                        remarks:remarks,
                        cloth_ids:cloth_ids
                    },
                    success:function(res){
                        if(res.success){
                            that_.text('修改');
                            that_.parent().parent().attr('id',res.info);
                        }

                    }
                })
            }
            if(that_.text() == '修改'){
                $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                    },
                    url:'/purchase/updatePurInfo',
                    type:'post',
                    dataType:'json',
                    data:{
                        cloth_code:cloth_code,
                        color:color,
                        demand:demand,
                        control:control,
                        purchase:purchase,
                        unit:unit,
                        purchase_id:purchase_id,
                        purchase_price:purchase_price,
                        total_price:total_price,
                        girard:surface_girard,
                        remarks:remarks,
                        id:id
                    },
                    success:function(res){
                        if(res.success){
                            $(".faceImg").attr("src","/img/laugh.png");
                            $(".oHandle").text("保存成功");
                            $(".oSuccess").show(0).delay(1000).hide(0);
                        }

                    }
                })
            }
        });
        //删除单行
        $(document).on('click','.remove_button',function () {
            var that_ = $(this);
            var id = that_.parent().parent().attr('id');
            var cloth_ids = that_.parent().siblings('.surface_to_girard').children().attr('abc');
            var getNow_money=$('.sum_of_money span').text();
            var getNow_num=$('.sum_of_num span').text();
            var getSelf_money=that_.parent().siblings('.surface_money').children().val();
            var getSelf_num=that_.parent().siblings('.surface_purchase').children().val();
            $(".oText").text("确认要删除吗？");
            $(".oMask").show();
            $(".oAnim").show();
            $(".oBtn0").attr('id','');
            $(".oBtn0").attr('id','oRefund');
            $(document).on('click','#oRefund',function(){
                $('.sum_of_money span').text(getNow_money-getSelf_money);
                $('.sum_of_num span').text(getNow_num-getSelf_num);
                if(id == undefined){
                    $(".oMask").hide();
                    $(".oAnim").hide();
                    that_.parent().parent().remove();
                    $(".faceImg").attr("src","/img/laugh.png");
                    $(".oHandle").text("删除成功");
                    $(".oSuccess").show(0).delay(1000).hide(0);
                }else{
                    $(".oMask").hide();
                    $(".oAnim").hide();
                    $.ajax({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                        },
                        url:'/purchase/deletePurInfo',
                        type:'post',
                        dataType:'json',
                        data:{
                            id:id,
                            cloth_ids:cloth_ids
                        },
                        success:function(res){
                            if(res.success = "true"){
                                that_.parent().parent().remove();
                                $(".faceImg").attr("src","/img/laugh.png");
                                $(".oHandle").text("删除成功");
                                $(".oSuccess").show(0).delay(1000).hide(0);
                            }
                        }
                    })
                }
            });
        });
        //监听采购量，改变每行总金额和总计数量
        $(document).on('change','.surface_purchase input',function () {
            var sumNum = 0;
            for(var m = 0;m<$('.surface_purchase input').length;m++){
                sumNum += parseInt($('.surface_purchase input').eq(m).val());
            }
            $('.sum_of_num span').text(sumNum);
            $(this).parent().siblings('.surface_money').children().val($(this).val()*$(this).parent().siblings('.surface_pur_price').children().val());
            sumMoney();
        });
        //监听总金额变动，合计金额
        $(document).on('change','.surface_money input',function () {
            sumMoney();
        });
        //监听采购价，计算每行总金额
        $(document).on('change','.surface_pur_price input',function(){
            $(this).parent().siblings('.surface_money').children().val($(this).val()*$(this).parent().siblings('.surface_purchase').children().val());
            sumMoney();
        });
        //点击服务款号获取款号用量
        var checkTrIndex;
        $(document).on('click','.surface_to_girard_child',function () {
            $('.allChoose').attr('checked',false);
            var surfaceId = $(this).parent().siblings('.cloth_code').children().val();
            var surfaceColor = $(this).parent().siblings('.surface_color').children().find("option:selected").text();
            if(surfaceId == "请选择" || surfaceColor == "请选择"){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请选择面料号或颜色！");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            checkTrIndex = $(this).parent().parent().index();
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                url:'/purchase/getClothNums',
                type:'post',
                dataType:'json',
                data:{
                    cloth_info:surfaceId,
                    color:surfaceColor
                },
                success:function(res){
                    if(res.success == true){
                        $('.model-container').show();
                        $('.model_data tbody').children().remove();
                        $.each(res.info,function (ida,obj) {
                            if(obj.is_actual == 1){
                                $('.model_data tbody').append("<tr><td><input type='checkbox' value='"+obj.id+"' class='allChoose'></td><td class='check_surface_girard'>"+obj.girard+"</td><td class='check_surface_num'>"+obj.remark+"</td><td class='check_surface_hand'><input type='text' disabled='disabled' value='"+obj.nums+"' /></td></tr>")
                            }else{
                                $('.model_data tbody').append("<tr><td><input type='checkbox' value='"+obj.id+"' class='allChoose'></td><td class='check_surface_girard'>"+obj.girard+"</td><td class='check_surface_num'>"+obj.remark+"</td><td class='check_surface_hand'><input type='text' value='"+obj.nums+"' /></td></tr>")
                            }
                        });
                    }
                }
            });
        });
        function cencelModel(){
            $('.model-container').hide();
        }
        //确认款号用量
        $(document).on('click','.save_girard',function () {
            var n=0;
            var getInredArray = "";
            var allSum = 0;
            var idSum = "";
            for(var i=0; i< $(".allChoose").length;i++){
                if($(".allChoose")[i].checked){
                    var id=$(".allChoose")[i].value;
                    var getInred = $(".allChoose").eq(i).parent().siblings('.check_surface_girard').text();
                    var singleSum = $(".allChoose").eq(i).parent().siblings('.check_surface_num').text()*$(".allChoose").eq(i).parent().siblings('.check_surface_hand').children().val();
                    n++;
                    getInredArray += getInred+",";
                    allSum += singleSum;
                    idSum += id+",";
                }
            }
            if(n<=0){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("没有选择项！");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            $('.pantone tbody').children().eq(checkTrIndex).children('.surface_to_girard').children().val(getInredArray.slice(0,-1));
            $('.pantone tbody').children().eq(checkTrIndex).children('.surface_to_girard').children().attr('abc',idSum.slice(0,-1));
            $('.pantone tbody').children().eq(checkTrIndex).children('.surface_purchase').children().val(allSum);
            var sumNum = 0;
            for(var m = 0;m<$('.surface_purchase input').length;m++){
                sumNum += parseInt($('.surface_purchase input').eq(m).val());
            }
            $('.sum_of_num span').text(sumNum);
            $(this).parent().siblings('.surface_money').children().val($(this).val()*$(this).parent().siblings('.surface_pur_price').children().val());
            $('.model-container').hide();
        });
        //底部保存按钮
        $('.save_all').click(function () {
            if($('#supplier').val() == '' || $('#season').val() == '' || $('#to_girard').val() == '' || $('#purchase_status').val() == '' || $('#purchase_date').val() == ''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请填写完整！带*号是必填的！");
                $(".oSuccess").show(0).delay(1500).hide(0);
                return false;
            }
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                url:'/purchase/addPurInfo',
                type:'post',
                dataType:'json',
                data:{
                    id:$.getUrlParam('id'),
                    supplier:$('#supplier').val(),
                    season:$('#season').val(),
                    num:$('.sum_of_num span').text(),
                    price:$('.sum_of_money span').text(),
                    to_girard:$('#to_girard').val(),
                    purchase_status:$('#purchase_status').val(),
                    contract_code:$('#contract_code').val(),
                    contract_name:$('#contract_name').val(),
                    remarks:$('#remarks').val(),
                    plan_code:$('.pur_list_id').text(),
                    purchase_date:$('#purchase_date').val(),
                },
                success:function (event) {
                    if(event.success){
                        $(".faceImg").attr("src","/img/laugh.png");
                        $(".oHandle").text("添加完成！");
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            })
        });
    </script>
@endsection
@section('script')
    @parent
    <script src="{{ env('CDN_DOMAIN_NAME') }}/js/jQuery-jcDate.js" type="text/javascript"></script>
    <script src="https://www.jq22.com/jquery/jquery-migrate-1.2.1.min.js"></script><!--JQ的版本引导-->
@endsection